<html>
<head>
<script>
function fib(number) {
var loop = [0, 1];
for (var i = 2; i < number; i++) {
loop[i] = loop[i-1]+ loop[i-2];
return loop[number-1];
}
document.getElementById("output").innerHTML
}
</script>
</head>
<body>
<input type text="text" id="txtloop" />
<input type="button" id="btnEnter" value="Enter" onclick="fib(txtloop.value)" />
<p id="output"></p>
</body>
</html>
<html>
<body>
<input type text="text" id="txtloop" />
<input type="button" id="btnEnter" value="Enter" onclick="fibonacci_series(txtloop.value)" />
<p id="output"></p>
<p id="demo"></p>
<script>
var fibonacci_series = function (n)
{
if (n==1)
{
var loop = [0, 1];
document.getElementById("output").innerHTML = loop;
return loop;
}
else
{
var s = fibonacci_series(n - 1);
s.push(s[s.length - 1] + s[s.length - 2]);
document.getElementById("output").innerHTML =s;
return s;
}
};
</script>
</body>
</html>
Run code snippet